package com.grocery.util;

import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.util.DigestUtils;

public class SYS {
    //文件上传保存路径
    public static final String FILE_UPLOAD_DIR="D:\\groceryupload\\";
    //上传文件访问前缀路径
    public static final String FILE_UPLOAD_URL="groceryfile";

    public static final String USER_LOGIN="USER_LOGIN";

    public static final String SYSTEM_LOGIN="SYSTEM_LOGIN";

    public static final int[] HEX_CHAR_ARR={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};



    public static String digest(String args) {
        //随机次数随机盐值
        String pass=args;
        int countIndex=(int)(Math.random()* HEX_CHAR_ARR.length);
        int saltIndex=(int)(Math.random()* HEX_CHAR_ARR.length);
        int count=HEX_CHAR_ARR[countIndex];
        char salt=(char)HEX_CHAR_ARR[saltIndex];
        char countChar=(char)count;
        pass=pass+salt;
        for(int i=0;i<count;i++){
            pass= DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String passwordText=salt+pass+countChar;
        return passwordText;
    }

    public static boolean checkPassword(String pass,String hexPass){
        char salt=hexPass.charAt(0);
        char countChar=hexPass.charAt(hexPass.length()-1);//次数
        pass=pass+salt;
        for(int i=0;i<countChar;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String text=salt+pass+countChar;
        return text.equals(hexPass);
    }

    public static void main(String[] args) {

    }
}
